package com.tc168.cp306.b06.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.tc168.cp306.b06.R;

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private DrawerLayout mDlNav;
    private NavigationView mNavView;
    private View mHeaderView;
    private ImageView mIvHeaderImg;
    private Toolbar mToolbar;
    private Button mBtnToCloud;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.tb_nav_bar);
        setSupportActionBar(mToolbar);
        mDlNav = (DrawerLayout) findViewById(R.id.dl_nav);
        mNavView = (NavigationView) findViewById(R.id.nav_view);
        mNavView.setCheckedItem(R.id.item_nav_menu_news);
        mNavView.setItemIconTintList(null);
        mNavView.setNavigationItemSelectedListener(this);
        mHeaderView = mNavView.getHeaderView(0);//获取头布局
        mIvHeaderImg = (ImageView) mHeaderView.findViewById(R.id.iv_nav_img);//获取头布局内 imageview 控件
        mIvHeaderImg.setOnClickListener(this);
        ActionBar lActionBar = getSupportActionBar();
        if (lActionBar != null) {
            lActionBar.setDisplayHomeAsUpEnabled(true);
            lActionBar.setHomeAsUpIndicator(R.mipmap.ic_launcher);
        }
        mBtnToCloud = (Button) findViewById(R.id.btn_is_cloud);
        mBtnToCloud.setOnClickListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Toast.makeText(NavActivity.this, item.getTitle(), Toast.LENGTH_LONG).show();
        mDlNav.closeDrawers();
        return true;
    }

    @Override
    public void onClick(View pView) {
        switch (pView.getId()) {
            case R.id.iv_nav_img:
                Toast.makeText(NavActivity.this, "点击 ImageView", Toast.LENGTH_LONG).show();
                break;

            case R.id.btn_is_cloud:
                startActivity(new Intent(NavActivity.this, NavButtomActivity.class));
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDlNav.openDrawer(Gravity.START);
                break;
        }
        return true;
    }
}
